package com.compass.doctor.ui.setting

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.view.KeyEvent
import com.compass.doctor.R
import com.compass.doctor.databinding.ActivityCustomScannerBinding
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.ext.onClick
import com.compass.framework.utils.LogUtil
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.getStringFromResource
import com.journeyapps.barcodescanner.CaptureManager
import com.journeyapps.barcodescanner.DecoratedBarcodeView

/**
 * Description： 自定义扫码界面
 * Date:2023/11/29 9:05
 */
class CustomCaptureActivity : BaseVBActivity<ActivityCustomScannerBinding>(),
    DecoratedBarcodeView.TorchListener {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, CustomCaptureActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var captureManager: CaptureManager


    override fun initView(savedInstanceState: Bundle?) {
        captureManager = CaptureManager(this, mBinding.zxingBarcodeScanner)
        captureManager.initializeFromIntent(intent, savedInstanceState)
        captureManager.decode()
        mBinding.zxingBarcodeScanner.setTorchListener(this)
        mBinding.zxingBarcodeScanner.decodeContinuous {
            it?.let {
                LogUtil.d("result==${it.text}")
                LogUtil.d("result==${it.result}")
                TipsToast.showTips(it.text)
            }
        }
        mBinding.switchFlashlight.onClick {
            switchFlashLight()
        }

    }

    override fun initData() {

    }

    private fun switchFlashLight() {
        if (getStringFromResource(R.string.turn_on_flashlight).equals(mBinding.switchFlashlight.text)) {
            mBinding.zxingBarcodeScanner.setTorchOn()
        } else {
            mBinding.zxingBarcodeScanner.setTorchOff()
        }
    }


    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        captureManager.onSaveInstanceState(outState)
    }

    override fun onResume() {
        super.onResume()
        captureManager.onResume()
    }

    override fun onPause() {
        super.onPause()
        captureManager.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        captureManager.onDestroy()
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return mBinding.zxingBarcodeScanner.onKeyDown(keyCode, event) || super.onKeyDown(
            keyCode,
            event
        );
    }

    override fun onTorchOn() {
        mBinding.switchFlashlight.text = getStringFromResource(R.string.turn_off_flashlight)
    }

    override fun onTorchOff() {
        mBinding.switchFlashlight.text = getStringFromResource(R.string.turn_on_flashlight)
    }


}